import logging
from dataclasses import dataclass
import importlib


# from utils import get_logger

def get_logger(logger_name, logger_file):
    # 创建Logger
    logger = logging.getLogger(logger_name)
    # logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    # 定义handler的输出格式
    formatter = logging.Formatter(
        "%(asctime)s - %(name)s - %(levelname)s: \n%(message)s\n",
        datefmt="%Y/%m/%d %H:%M:%S",
    )
    # 避免重复添加处理器
    if not any(isinstance(h, logging.FileHandler) for h in logger.handlers):
        handler = logging.FileHandler(logger_file)
        handler.setLevel(logging.INFO)

        formatter = logging.Formatter(
            "%(asctime)s - %(name)s - %(levelname)s: \n%(message)s\n",
            datefmt="%Y/%m/%d %H:%M:%S",
        )
        handler.setFormatter(formatter)
        logger.addHandler(handler)

        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)

        ch.setFormatter(formatter)
        logger.addHandler(ch)

    # 写入日志
    # logger.debug("debug message")
    # logger.info("info message")
    # logger.warning("warning message")
    # logger.error("error message")
    # logger.critical("critical message")
    return logger


raw_industry_data = "/media/jie/新加卷/pku_data/industry_csv"

address = {"prov": ["北京", "天津", "河北"]}


# address = {
#     "city": "雄安新区",
# }


@dataclass
class BertConfig:
    # epoch = 3
    # learning_rate = 2e-5

    # 数据集
    bert_binary_train = "output/{NAME}/bert_binary_train"
    bert_multi_train = "output/{NAME}/bert_multi_train"

    # 模型训练输出结果
    output_binary_dir = "output/{NAME}/bert/bert_binary_output"
    output_multi_dir = "output/{NAME}/bert/bert_multi_output"
    model_name = "bert-base-chinese"

    def format(self, item):
        return item.format(NAME=self.NAME)

    def __init__(self, name):
        self.NAME = name
        self.bert_binary_train = self.format(self.bert_binary_train)
        self.bert_multi_train = self.format(self.bert_multi_train)

        self.output_binary_dir = self.format(self.output_binary_dir)
        self.output_multi_dir = self.format(self.output_multi_dir)


@dataclass
class StaticValues:
    logger = None
    home_folder = "output/{NAME}"
    chinese_name: str = ""
    KW_CSV: str = "output/{NAME}/{NAME}_kw.csv"

    BINARY_VLLM_OBJ: str = "output/{NAME}/binary_{NAME}.pkl"

    # 包含某个产业链所有的企业
    INDUSTRY_CSV: str = "output/{NAME}/{NAME}.csv"

    INDUSTRY_VLLM_OBJ: str = "output/{NAME}/industry_{NAME}.pkl"
    INDUSTRY_VLLM_MULTI_OBJ: str = "output/{NAME}/industry_{NAME}_multi.pkl"

    # INDUSTRY__CSV: str = "output/{NAME}/{NAME}分类.csv"
    logger_file: str = "output/{NAME}/log.txt"

    human_folder = "output/human/{chinese_name}/"

    # kw
    KEY_WORDS: list = None

    # prompt
    binary_cls_prompt: str = ""
    multi_cls_prompt: str = ""

    ## local_dataset, load_from_disk
    # llm, bert
    """
        不存在，则首先创建，添加大模型的 reason， label, 等等
    """
    industry_dataset: str = "output/{NAME}/industry_dataset"
    industry_multi_dataset: str = "output/{NAME}/industry_multi_dataset"

    LABEL_NAME: object = None

    def __init__(self, name):
        self.NAME = name
        self.home_folder = self.home_folder.format(NAME=name)
        self.KW_CSV = self.KW_CSV.format(NAME=name)
        self.BINARY_VLLM_OBJ = self.BINARY_VLLM_OBJ.format(NAME=name)
        self.INDUSTRY_VLLM_OBJ = self.INDUSTRY_VLLM_OBJ.format(NAME=name)
        self.INDUSTRY_VLLM_MULTI_OBJ = self.INDUSTRY_VLLM_MULTI_OBJ.format(NAME=name)

        self.industry_dataset = self.industry_dataset.format(NAME=name)
        self.industry_multi_dataset = self.industry_multi_dataset.format(NAME=name)

        # 最终返回给用户的结果
        self.INDUSTRY_CSV = self.INDUSTRY_CSV.format(NAME=name)
        self.logger_file = self.logger_file.format(NAME=name)

        module = importlib.import_module(f"industries.{self.NAME}")
        self.binary_cls_prompt = module.binary_cls_prompt
        self.multi_cls_prompt = module.industry_cls_prompt
        if not self.logger:
            self.logger = get_logger(self.NAME, self.logger_file)

        self.LABEL_NAME = module.LABEL_NAME
        self.KEY_WORDS = module.KEY_WORDS
        self.chinese_name = module.chinese_name

        # 用于输出给用户的文件夹
        self.human_folder = self.human_folder.format(chinese_name=self.chinese_name)
        self.bert_config = BertConfig(name=name)


if __name__ == "__main__":
    # sv = StaticValues("hydrogen")
    sv = StaticValues("internet")
    print(sv)
    print(sv.human_folder)
